package xuperchain

import (
	"encoding/json"
	"fmt"

	"github.com/xuperchain/xuper-sdk-go/v2/account"
	"github.com/xuperchain/xuper-sdk-go/v2/xuper"
)

type paper struct {
	Level string `json:"0"`
}

func HasPaper(paperName string) (result string) {
	account, err := account.GetAccountFromFile(path, pwd)
	if err != nil {
		fmt.Printf("GetAccountFromFile err: %v\n", err)
		return
	}
	fmt.Printf("privatekey Account address: %v\n", account.Address)

	err = account.SetContractAccount(contractAccount)
	if err != nil {
		panic(err)
	}

	xchainClient, err := xuper.New(nodeAddress)
	if err != nil {
		panic(err)
	}
	contractName := "xc_filestorage8"

	// 检查论文是否存在
	argsHasPaper := map[string]string{
		"_name": paperName,
	}
	fmt.Print(paperName)
	txHasPaper, err := xchainClient.QueryEVMContract(account, contractName, "hasPaper", argsHasPaper)
	if err != nil {
		panic(err)
	}
	var response []paper
	err = json.Unmarshal([]byte(txHasPaper.ContractResponse.Body), &response)
	if err != nil {
		panic(err)
	}

	return response[0].Level
}
